#include "Cards.h"

#include <QLayout>
#include <QDebug>
#include "Player.h"
#include <QApplication>
#include "Translator.h"

Cards::Cards(QWidget* parent) : QWidget(parent), m_soldier(QApplication::applicationDirPath () + "/Ress/Icons/soldier.png"), m_horse(QApplication::applicationDirPath () + "/Ress/Icons/horse.png"), m_canon(QApplication::applicationDirPath () + "/Ress/Icons/canon.png")
{
    m_bonusCount = 2;

    QVBoxLayout *layout = new QVBoxLayout(this);
    {
        m_model = new QStandardItemModel(this);
        m_view = new QListView(this);
            m_view->setModel(m_model);
            m_view->setSelectionMode(QAbstractItemView::ExtendedSelection);
            m_view->setEditTriggers(QAbstractItemView::NoEditTriggers);
            layout->addWidget(m_view);
        m_bonusLabel = new QLabel(tr("Prochain bonus : %1 soldats.").arg(m_bonusCount), this);
            layout->addWidget(m_bonusLabel);
        m_bonus = new QPushButton(tr("BONUUUUUUUUUS !"), this);
            layout->addWidget(m_bonus);
            connect(m_bonus, SIGNAL(clicked()), this, SLOT(bonus()));
    }
    setLayout(layout);
}

void Cards::bonus()
{
    QModelIndexList list = m_view->selectionModel()->selectedIndexes();
    if(list.size() != 3)
        return;

    QList <Card* > m_selected;
    for (int i = 0 ; i < list.size(); i++)
    {
        QString elem = getTranslator()->unTranslate(m_model->data(list[i], Qt::DisplayRole).toString());
        for(int j = 0; j < m_cards.size(); ++j)
        {
            if(m_cards[j]->state->name() == elem)
            {
                m_selected.append(m_cards[j]);
                break;
            }
        }
    }

    Card* m1 = m_selected[0];
    Card* m2 = m_selected[1];
    Card* m3 = m_selected[2];

    bool ok = false;
    if(m1->type == m2->type && m2->type == m3->type)
        ok = true;
    if(m1->color == m2->color && m2->color == m3->color)
        ok = true;
    if(m1->type != m2->type && m2->type != m3->type && m1->type != m3->type)
        ok = true;

    if(!ok)
        return;

    m1->state->player()->addReinforcement(m_bonusCount);
    emit reinforcementsAdded();

    m_bonusCount *= 2;
    if(m_bonusCount > 2048)
        m_bonusCount = 2048;
    m_bonusLabel->setText(tr("Prochain bonus : %1 soldats.").arg(m_bonusCount));

    for(int i = 0; i < m_selected.size(); ++i)
    {
        m_cards.removeOne(m_selected[i]);
        delete m_selected[i];
    }
    refresh();
}

Cards::~Cards()
{
    for(size_t i = 0; i < (unsigned)m_cards.size(); ++i)
    {
        delete m_cards[i];
    }
}
bool Cards::playerGotCard(const State* state, const Player* ply) const
{
    for(int i = 0; i < m_cards.size(); ++i)
    {
        if(m_cards[i]->state == state && m_cards[i]->owner == ply)
            return true;
    }
    return false;
}

void Cards::addCard(State* state)
{
    m_cards.append(new Card(state, state->player()));
}

bool Cards::changeCardOwner(const State* state, Player* owner)
{
    for(int i = 0; i < m_cards.size(); ++i)
    {
        if(m_cards[i]->state == state)
        {
            m_cards[i]->owner = owner;
            refresh();
            return false;
        }
    }

    return true;
}

void Cards::refresh()
{
    m_model->clear();
    for(int i = 0; i < m_cards.size(); ++i)
    {
        if(m_cards[i]->owner != m_watcher)
            continue;

        QStandardItem * it = new QStandardItem;

        if(m_cards[i]->type == HORSE)
            it->setData( m_horse, Qt::DecorationRole);
        else if(m_cards[i]->type == SOLDIER)
            it->setData(m_soldier, Qt::DecorationRole);
        else
            it->setData(m_canon, Qt::DecorationRole);

        it->setData(m_cards[i]->color, Qt::ForegroundRole);

        it->setData(getTranslator()->translate(m_cards[i]->state->name()), Qt::DisplayRole);

        m_model->appendRow(it);
    }
}
